package chen.com.StuPro;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentText {

    private static final String ADD_STUDENT ="1";
    private static final String DELETE_STUDENT ="2";
    private static final String UPDATE_STUDENT ="3";
    private static final String QUERY_STUDENT ="4";
    private static final String EXIT ="5";

    //设置静态变量
    private static ArrayList<Student> list=new ArrayList<>();
//    //静态代码块
//    static{
//        list.add(13,);
//    }
//


    public static void main(String[] args) {


        Scanner sc=new Scanner(System.in);
        String index;
        //生成选择界面
        while(true)
        {
            System.out.println("-------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择：");
            index=sc.next();
            switch(index)
            {
                //在case 后面的敞亮阅读性不高
                case ADD_STUDENT->{

                  addStudent(list);
              }
                case DELETE_STUDENT->{

                 deleteStudent(list);
              }
                case UPDATE_STUDENT->{

                   updateStudent(list);
              }
                case QUERY_STUDENT-> {

                  queryStudent(list);
              }
              case EXIT->{
                  System.out.println("5.退出");
                  System.exit(0);//停止虚拟机运行
              }
              default-> System.out.println("没有这个选项");


            }
        }
    }


    public static int indexid(ArrayList<Student> list,String id)
    {
        for (int i = 0; i <list.size() ; i++) {
            Student stu1=list.get(i);
            if(stu1.getid().equals(id))
            {
                return i;
            }

        }
        return -1;
    }
    public static boolean contains(ArrayList<Student> list,String id)
    {
//        //判断当前id是否已经存在
//        for (int i = 0; i <list.size() ; i++) {
//            Student stu1=list.get(i);
//            if(stu1.getid().equals(id))
//            {
//                return false;
//            }
//
//        }
//        return true;
        return indexid(list,id)>=0;
    }
    public static void addStudent(ArrayList<Student> list)
     {
         System.out.println("1：添加学生");
         Student stu=new Student();
         Scanner sc=new Scanner(System.in);
         while (true) {
             System.out.println("请输入id");
             String id1=sc.next();
             if(contains(list,id1))
             {
                 //System.out.println("学号不存在，输入有效");

                 System.out.println("学号已存在，请重新输入");
             }
             else
             {
                 stu.setid(id1);
                 break;
             }

         }


         System.out.println("请输入姓名");
         String name1=sc.next();
         stu.setName(name1);
         System.out.println("请输入年龄");
         int age1=sc.nextInt();
         stu.setAge(age1);
         System.out.println("请输入家庭住址");
         String address1=sc.next();
         stu.setAddress(address1);
         list.add(stu);

         System.out.println("学生信息添加成功");
     }

    public static void deleteStudent(ArrayList<Student> list)
    {
        System.out.println("2：删除学生");

        //根据学生的id来删除学生
        System.out.println("输入要删除学生的id");
        Scanner sc=new Scanner(System.in);
        String id=sc.next();//输入要查询的id

       int index=indexid(list,id);

       if(index>=0) {
           list.remove(index);
           System.out.println("学生信息删除成功");
       }
       else
           System.out.println("你查询的学生信息并不存在");

    }

    public static void updateStudent(ArrayList<Student> list)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("3：修改学生");
        //根据输入的学生id
        String ids=sc.next();
        int index=indexid(list,ids);


        if(index>=0)
        {
            System.out.println("找到学生的信息，下面开始修改");
            Student stu=list.get(index);
            System.out.println("请输入要修改的姓名");
            String name=sc.next();
            stu.setName(name);
            System.out.println("请输入要修改的年龄");
            int age=sc.nextInt();
            stu.setAge(age);
            System.out.println("请输入要修改的家庭住址");
            String address=sc.next();
            stu.setAddress(address);

        }
        else
        {
            System.out.println("并未找到对应id的学生信息");
        }


    }

    public static void queryStudent(ArrayList<Student> list)
    {
        System.out.println("4：查询学生");
        if(list.size()==0)
        {
            System.out.println("当前无学生信息，请添加后查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i <list.size() ; i++) {
            Student stu=list.get(i);
            System.out.println(stu.getid()+","+stu.getName()+","+stu.getAge()+","+stu.getAddress());
        }
    }
}
